
import xml.dom.minidom

def fromXMLGetData(path):
    """
    :param path: xml path
    :return: a list, save many son-lists, one son-lists is a trash location, has name/xmin/ymin/xmax/ymax
    """
    maxTotalDetections = 300
    needGetData = ["xmin", "ymin", "xmax", "ymax", "name"]

    dom = xml.dom.minidom.parse(path)
    root = dom.documentElement
    root = root.getElementsByTagName("object")

    res = []
    for i in root:
        tempList = []
        for elem in needGetData:
            tempNode = i.getElementsByTagName(elem)[0]
            if tempNode.nodeName == "name":
                tempName = tempNode.firstChild.data
                shouldAppend = 0
                if tempName == "trash1":
                    shouldAppend = 1
                elif tempName == "trash2":
                    shouldAppend = 2
                tempList.append(shouldAppend)
            elif tempNode.nodeName == "ymin" or tempNode.nodeName == "ymax":
                tempSize = float(tempNode.firstChild.data)
                tempSize = (tempSize + 280.) / 2.5
                tempList.append(tempSize)
            else:
                tempSize = float(tempNode.firstChild.data)
                tempSize = tempSize / 2.5
                tempList.append(tempSize)




        assert len(tempList) == 5
        res += tempList

    res += [0] * (maxTotalDetections*5 - len(res))
    assert len(res) == maxTotalDetections*5 , print(path)
    return res


if __name__ == '__main__':
    data = fromXMLGetData('./data/trainxml/114.xml')
    print(data.__len__())
    print(data)



